﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DAIVIETPROJECT
{
    public partial class frm_main : Form
    {
        public frm_main()
        {
            InitializeComponent();
        }

        private void frm_main_Load(object sender, EventArgs e)
        {
            try
            {
            loadmenu(e);
            string manv = "01";
            daivietDataContext context = new daivietDataContext();
            IEnumerable<DM_nhanvien> nhanviens = from c in context.DM_nhanviens where c.manv.Equals(manv) select c;
            if (nhanviens.Count() > 0)
            {
                this.tsstennhanvien.Text = nhanviens.ElementAt(0).hoten.ToUpper();
                DateTime serverdate=context.p_getdate().ElementAt(0).serverdatetime;
                string ngaylamviec="";
                if (serverdate.Day < 10)
                {
                    ngaylamviec = ngaylamviec + "0" + serverdate.Day+"/";
                }
                else
                {
                    ngaylamviec = ngaylamviec  + serverdate.Day+"/";
                }
                if (serverdate.Month < 10)
                {
                    ngaylamviec = ngaylamviec + "0" + serverdate.Month+"/";
                }
                else
                {
                    ngaylamviec = ngaylamviec + serverdate.Month+"/";
                }
                ngaylamviec = ngaylamviec + serverdate.Year;

                this.tssngaylamviec.Text = ngaylamviec;
            }
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
        public void loadmenu(EventArgs e)
        {
            daivietDataContext context = new daivietDataContext();
            IEnumerable<HT_menu> list = from c in context.HT_menus where c.cap.Equals(1)select c;

            HT_menu mn;
           
            for (int i = 0; i < list.Count(); i++)
            {
                mn=list.ElementAt(i);
                //ToolStripItem item;
                //item.Text = mn.tenmenu;
                //item.Name = mn.mamenu;
                ToolStripMenuItem parent = new ToolStripMenuItem();
                parent.Text = mn.tenmenu;
                parent.Name = mn.mamenu;
               
                //this.mn_mainmenu.Items[i].Name = mn.mamenu;
                IEnumerable<HT_menu> childlist=from c in context.HT_menus where c.cap.Equals(2) where c.mamenucha.Equals(mn.mamenu) select c;
                HT_menu cmn;
              
                for (int j = 0; j < childlist.Count(); j++)
                {   cmn=childlist.ElementAt(j);
                    ToolStripMenuItem child =new ToolStripMenuItem();
                    child.Name=cmn.mamenu;
                    child.Text=cmn.tenmenu;
                  


                    
                    child.Click += new System.EventHandler(sub_mainmenu_Click);

                     parent.DropDownItems.Add(child);
                    
                }
                this.mn_mainmenu.Items.Add(parent);

                
              

            }


        }

        private void mn_mainmenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            //this.label1.Text = e.ClickedItem.Name;
           
           
            //MessageBox.Show(e.ClickedItem.Name);    

        }
        private void sub_mainmenu_Click(Object sender, EventArgs e)
        {
            daivietDataContext context=new daivietDataContext();
            //MessageBox.Show(sender.ToString());
            string tenmenu = sender.ToString();
            IEnumerable<HT_menu> l=from c in context.HT_menus where c.tenmenu.Equals(tenmenu)select c;
            if (l.Count() > 0)
            {
                HT_menu m = l.ElementAt(0);
                string modulename, formname;
                modulename = m.module;
                formname = m.form;
               
                switch (modulename)
                { 
                    case "MODULE_DANHMUC":
                        MODULE_DANHMUC.cls_danhmuc clsdm = new MODULE_DANHMUC.cls_danhmuc();
                        clsdm.ShowForm(formname);
                        break;
                    case "MODULE_KCS":
                        MODULE_KCS.cls_kcs clskcs = new MODULE_KCS.cls_kcs();
                        clskcs.ShowForm(formname);
                        break;
                    case "MODULE_KETOAN":
                        MODULE_KETOAN.cls_ketoan clskt = new MODULE_KETOAN.cls_ketoan();
                        clskt.ShowForm(formname);
                        break;
                    case "MODULE_NHAPXUAT":
                        MODULE_NHAPXUAT.cls_nhapxuat clsnx = new MODULE_NHAPXUAT.cls_nhapxuat();
                        clsnx.ShowForm(formname);
                        break;

                    case "MODULE_QUANTRIHETHONG":
                        MODULE_QUANTRIHETHONG.cls_quantrihethong clsqtht = new MODULE_QUANTRIHETHONG.cls_quantrihethong();
                        clsqtht.ShowForm(formname);
                        break;
                }
               // MessageBox.Show(m.form); 

            }

            //MessageBox.Show(e.ToString());
           
            
        }

        private void reportDocument1_InitReport(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            MODULE_TIENICH.cls_tienich cls = new MODULE_TIENICH.cls_tienich();
            cls.ShowForm("frm_report");
        }

       
    }
}
